package cn.easybuy.filter;

import cn.easybuy.util.Constants;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/admin/*")
public class LoginFilter implements Filter {

    @Value("${server.servlet.context-path}")
    private String ctx;

    //判断用户是否登录,登录则可以访问订单列表,否跳到403页面
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest =(HttpServletRequest) request;
        if (httpServletRequest.getSession().getAttribute(Constants.LOGIN_USER)!=null){
            chain.doFilter(request,response);
        }else{
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            httpServletResponse.sendRedirect(ctx+"/403.jsp");
        }
    }
}
